home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / abbrev.c next >
C/C++ Source or Header  |  1991-12-02  |  16KB  |  523 lines

  1. /* Primitives for word-abbrev mode.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #undef NULL
  24. #include "lisp.h"
  25. #include "commands.h"
  26. #include "buffer.h"
  27. #include "window.h"
  28.  
  29. /* An abbrev table is an obarray.
  30.  Each defined abbrev is represented by a symbol in that obarray
  31.  whose print name is the abbreviation.
  32.  The symbol's value is a string which is the expansion.
  33.  If its function definition is non-nil, it is called
  34.   after the expansion is done.
  35.  The plist slot of the abbrev symbol is its usage count. */
  36.  
  37. /* List of all abbrev-table name symbols:
  38.  symbols whose values are abbrev tables.  */
  39.  
  40. Lisp_Object Vabbrev_table_name_list;
  41.  
  42. /* The table of global abbrevs.  These are in effect
  43.  in any buffer in which abbrev mode is turned on. */
  44.  
  45. Lisp_Object Vglobal_abbrev_table;
  46.  
  47. /* The local abbrev table used by default (in Fundamental Mode buffers) */
  48.  
  49. Lisp_Object Vfundamental_mode_abbrev_table;
  50.  
  51. /* Set nonzero when an abbrev definition is changed */
  52.  
  53. int abbrevs_changed;
  54.  
  55. int abbrev_all_caps;
  56.  
  57. /* Non-nil => use this location as the start of abbrev to expand
  58.  (rather than taking the word before point as the abbrev) */
  59.  
  60. Lisp_Object Vabbrev_start_location;
  61.  
  62. /* Buffer that Vabbrev_start_location applies to */
  63. Lisp_Object Vabbrev_start_location_buffer;
  64.  
  65. /* The symbol representing the abbrev most recently expanded */
  66.  
  67. Lisp_Object Vlast_abbrev;
  68.  
  69. /* A string for the actual text of the abbrev most recently expanded.
  70.    This has more info than Vlast_abbrev since case is significant.  */
  71.  
  72. Lisp_Object Vlast_abbrev_text;
  73.  
  74. /* Character address of start of last abbrev expanded */
  75.  
  76. int last_abbrev_point;
  77.  
  78. extern Lisp_Object oblookup ();
  79.  
  80. DEFUN ("make-abbrev-table", Fmake_abbrev_table, Smake_abbrev_table, 0, 0, 0,
  81.   "Create a new, empty abbrev table object.")
  82.   ()
  83. {
  84.   return Fmake_vector (make_number (59), make_number (0));
  85. }
  86.  
  87. DEFUN ("clear-abbrev-table", Fclear_abbrev_table, Sclear_abbrev_table, 1, 1, 0,
  88.   "Undefine all abbrevs in abbrev table TABLE, leaving it empty.")
  89.   (table)
  90.      Lisp_Object table;
  91. {
  92.   int i, size;
  93.  
  94.   CHECK_VECTOR (table, 0);
  95.   size = XVECTOR (table)->size;
  96.   abbrevs_changed = 1;
  97.   for (i = 0; i < size; i++)
  98.     XVECTOR (table)->contents[i] = make_number (0);
  99.   return Qnil;
  100. }
  101.  
  102. DEFUN ("define-abbrev", Fdefine_abbrev, Sdefine_abbrev, 3, 5, 0,
  103.   "Define an abbrev in TABLE named NAME, to expand to EXPANSION or call HOOK.\n\
  104. NAME and EXPANSION are strings.  HOOK is a function or nil.\n\
  105. To undefine an abbrev, define it with EXPANSION = nil")
  106.   (table, name, expansion, hook, count)
  107.      Lisp_Object table, name, expansion, hook, count;
  108. {
  109.   Lisp_Object sym, oexp, ohook, tem;
  110.   CHECK_VECTOR (table, 0);
  111.   CHECK_STRING (name, 1);
  112.   CHECK_STRING (expansion, 2);
  113.   if (NULL (count))
  114.     count = make_number (0);
  115.   else
  116.     CHECK_NUMBER (count, 0);
  117.  
  118.   sym = Fintern (name, table);
  119.  
  120.   oexp = XSYMBOL (sym)->value;
  121.   ohook = XSYMBOL (sym)->function;
  122.   if (!((EQ (oexp, expansion)
  123.      || (XTYPE (oexp) == Lisp_String && XTYPE (expansion) == Lisp_String
  124.          && (tem = Fstring_equal (oexp, expansion), !NULL (tem))))
  125.     &&
  126.     (EQ (ohook, hook)
  127.      || (tem = Fequal (ohook, hook), !NULL (tem)))))
  128.     abbrevs_changed = 1;
  129.  
  130.   Fset (sym, expansion);
  131.   Ffset (sym, hook);
  132.   Fsetplist (sym, count);
  133.  
  134.   return name;
  135. }
  136.  
  137. DEFUN ("define-global-abbrev", Fdefine_global_abbrev, Sdefine_global_abbrev, 2, 2,
  138.   "sDefine global abbrev: \nsExpansion for %s: ",
  139.   "Define ABBREV as a global abbreviation for EXPANSION.")
  140.   (name, expansion)
  141.      Lisp_Object name, expansion;
  142. {
  143.   Fdefine_abbrev (Vglobal_abbrev_table, Fdowncase (name),
  144.           expansion, Qnil, make_number (0));
  145.   return name;
  146. }
  147.  
  148. DEFUN ("define-mode-abbrev", Fdefine_mode_abbrev, Sdefine_mode_abbrev, 2, 2,
  149.   "sDefine mode abbrev: \nsExpansion for %s: ",
  150.   "Define ABBREV as a mode-specific abbreviation for EXPANSION.")
  151.   (name, expansion)
  152.      Lisp_Object name, expansion;
  153. {
  154.   if (NULL (current_buffer->abbrev_table))
  155.     error ("Major mode has no abbrev table");
  156.  
  157.   Fdefine_abbrev (current_buffer->abbrev_table, Fdowncase (name),
  158.           expansion, Qnil, make_number (0));
  159.   return name;
  160. }
  161.  
  162. DEFUN ("abbrev-symbol", Fabbrev_symbol, Sabbrev_symbol, 1, 2, 0,
  163.   "Return the symbol representing abbrev named ABBREV.\n\
  164. Value is nil if that abbrev is not defined.\n\
  165. Optional second arg TABLE is abbrev table to look it up in.\n\
  166. Default is try buffer's mode-specific abbrev table, then global table.")
  167.   (abbrev, table)
  168.      Lisp_Object abbrev, table;
  169. {
  170.   Lisp_Object sym;
  171.   CHECK_STRING (abbrev, 0);
  172.   if (!NULL (table))
  173.     sym = Fintern_soft (abbrev, table);
  174.   else
  175.     {
  176.       sym = Qnil;
  177.       if (!NULL (current_buffer->abbrev_table))
  178.     sym = Fintern_soft (abbrev, current_buffer->abbrev_table);
  179.       if (NULL (XSYMBOL (sym)->value))
  180.     sym = Qnil;
  181.       if (NULL (sym))
  182.     sym = Fintern_soft (abbrev, Vglobal_abbrev_table);
  183.     }
  184.   if (NULL (XSYMBOL (sym)->value)) return Qnil;
  185.   return sym;
  186. }
  187.  
  188. DEFUN ("abbrev-expansion", Fabbrev_expansion, Sabbrev_expansion, 1, 2, 0,
  189.   "Return the string that ABBREV expands into in the current buffer.\n\
  190. Optionally specify an abbrev table; then ABBREV is looked up in that table only.")
  191.   (abbrev, table)
  192.      Lisp_Object abbrev, table;
  193. {
  194.   Lisp_Object sym;
  195.   sym = Fabbrev_symbol (abbrev, table);
  196.   if (NULL (sym)) return sym;
  197.   return Fsymbol_value (sym);
  198. }
  199.  
  200. /* Expand the word before point, if it is an abbrev.
  201.   Returns 1 if an expansion is done. */
  202.  
  203. DEFUN ("expand-abbrev", Fexpand_abbrev, Sexpand_abbrev, 0, 0, "",
  204.   "Expand the abbrev before point, if it is an abbrev.\n\
  205. Effective when explicitly called even when abbrev-mode is not enabled.\n\
  206. Returns t if expansion took place.")
  207.   ()
  208. {
  209.   char buffer[200];
  210.   register char *p = buffer;
  211.   register int wordstart, idx;
  212.   int uccount = 0, lccount = 0;
  213.   register Lisp_Object sym;
  214.   Lisp_Object expansion, hook, tem;
  215.  
  216.   if (XBUFFER (Vabbrev_start_location_buffer) != current_buffer)
  217.     Vabbrev_start_location = Qnil;
  218.   if (!NULL (Vabbrev_start_location))
  219.     {
  220.       tem = Vabbrev_start_location;
  221.       CHECK_NUMBER_COERCE_MARKER (tem, 0);
  222.       wordstart = XINT (tem);
  223.       Vabbrev_start_location = Qnil;
  224.       if (FETCH_CHAR (wordstart) == '-')
  225.     del_range (wordstart, wordstart + 1);
  226.     }
  227.   else
  228.     wordstart = scan_words (point, -1);
  229.  
  230.   if (!wordstart || point - wordstart >= sizeof buffer || point <= wordstart)
  231.     return Qnil;
  232.  
  233.   for (idx = wordstart; idx < point; idx++)
  234.     {
  235.       register int c = FETCH_CHAR (idx);
  236.       if (UPPERCASEP (c))
  237.     c = DOWNCASE (c), uccount++;
  238.       else if (! NOCASEP (c))
  239.     lccount++;
  240.       *p++ = c;
  241.     }
  242.  
  243.   if (XTYPE (current_buffer->abbrev_table) == Lisp_Vector)
  244.     sym = oblookup (current_buffer->abbrev_table, buffer, p - buffer);
  245.   else
  246.     XFASTINT (sym) = 0;
  247.   if (XTYPE (sym) == Lisp_Int ||
  248.       NULL (XSYMBOL (sym)->value))
  249.     sym = oblookup (Vglobal_abbrev_table, buffer, p - buffer);
  250.   if (XTYPE (sym) == Lisp_Int ||
  251.       NULL (XSYMBOL (sym)->value))
  252.     return Qnil;
  253.  
  254.   if (FROM_KBD && !EQ (minibuf_window, selected_window))
  255.     {
  256.       SET_PT (wordstart + p - buffer);
  257.       Fundo_boundary ();
  258.     }
  259.   SET_PT (wordstart);
  260.   Vlast_abbrev_text
  261.     = Fbuffer_substring (make_number (point),
  262.              make_number (point + (p - buffer)));
  263.   del_range (point, point + (p - buffer));
  264.  
  265.   /* Now sym is the abbrev symbol. */
  266.   Vlast_abbrev = sym;
  267.   last_abbrev_point = point;
  268.  
  269.   if (XTYPE (XSYMBOL (sym)->plist) == Lisp_Int)
  270.     XSETINT (XSYMBOL (sym)->plist,
  271.          XINT (XSYMBOL (sym)->plist) + 1);    /* Increment use count */
  272.  
  273.   expansion